//
//  LGConnectSuccessVC.m
//  LGBlueToothDemo1
//
//  Created by liangang zhan on 2019/1/22.
//  Copyright © 2019年 ZnLG. All rights reserved.
//

#import "LGConnectSuccessVC.h"

@interface LGConnectSuccessVC ()<CBPeripheralDelegate>
@property (strong, nonatomic)UITextView *textView;
@end

@implementation LGConnectSuccessVC

- (void)viewDidLoad {
 
    [super viewDidLoad];
    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 80, kScreenWidth-20, 300)];
    [self.view addSubview:textView];
    self.textView = textView;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.succeedPeripheral.name;
    self.succeedPeripheral.delegate = self;
    //外围设备开始寻找服务
    [self.succeedPeripheral discoverServices:nil];
}

- (void)dealloc {
    NSLog(@"======设备页面销毁了");
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.succeedPeripheral.delegate = nil;
}

#pragma mark - CBPeripheralDelegate 代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{
    NSLog(@"已发现可用服务...");
    if(error){
        NSLog(@"外围设备寻找服务过程中发生错误，错误信息：%@",error.localizedDescription);
    }
    CBUUID *serviceUUID=[CBUUID UUIDWithString:kServiceUUID];
    //遍历查找到的服务
    for (CBService *service in peripheral.services) {
        NSLog(@"查看服务%@",service.UUID);
        if([service.UUID isEqual:serviceUUID]){
            //外围设备查找指定服务中的特征
            [peripheral discoverCharacteristics:nil forService:service];
        }
    }
}

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
    NSLog(@"已发现可用特征...");
    if (error) {
        NSLog(@"外围设备寻找特征过程中发生错误，错误信息：%@",error.localizedDescription);
    }
    //遍历服务中的特征
    CBUUID *serviceUUID=[CBUUID UUIDWithString:kServiceUUID];
    CBUUID *characteristicUUID=[CBUUID UUIDWithString:kCharacteristicUUID];
    if([service.UUID isEqual:serviceUUID]){
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID isEqual:characteristicUUID]) {
                //                NSLog(@"特征值为：%@",[[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding]);
                //                if (characteristic.properties ==CBCharacteristicPropertyRead) {
                //
//                [peripheral readValueForCharacteristic:characteristic];
                //                }
                NSLog(@"订阅特征");
                [peripheral setNotifyValue:YES forCharacteristic:characteristic];
            }
            //            else if ([characteristic.UUID isEqual:characteristicUUIDTWO]) {
            //                if(characteristic.properties == CBCharacteristicPropertyNotify){
            //                }
            //            }
            
        }
    }
}

#pragma mark - 蓝牙数据接收
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
    NSLog(@"======接收特征值");
    [self writeToLog:@"======接收特征值"];
    if (error) {
        NSString *str = [NSString stringWithFormat:@"更新特征值时发生错误，错误信息：%@",error.localizedDescription];
        NSLog(@"%@",str);
        [self writeToLog:str];
        return;
    }
        CBUUID *characteristicUUID=[CBUUID UUIDWithString:kCharacteristicUUID];
//    CBUUID *characteristicUUIDTWO=[CBUUID UUIDWithString:kCharacteristicUUIDTWO];
    if ([characteristic.UUID isEqual:characteristicUUID]) {
//        if( characteristic.properties == CBCharacteristicPropertyRead){
            NSLog(@"======1读取特征值为：%@",[[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding]);
            if (characteristic.value) {
                
                NSString *string = [NSString stringWithFormat:@"======1读取特征值为：%@",[[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding]];
                [self writeToLog:string];
                NSLog(@"%@",string);
            }
//        }

    }
//    else if ([characteristic.UUID isEqual:characteristicUUIDTWO]) {
//        if( characteristic.properties == CBCharacteristicPropertyNotify){
//            NSLog(@"======2更新特征值为：%@",[[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding]);
//            if (characteristic.value) {
//                
//                NSString *string = [NSString stringWithFormat:@"======2读取特征值为：%@",[[NSString alloc]initWithData:characteristic.value encoding:NSUTF8StringEncoding]];
//                [self writeToLog:string];
//                NSLog(@"%@",string);
//            }
//        }
//    }
    
}

-(void)writeToLog:(NSString *)info{
    self.textView.text=[NSString stringWithFormat:@"%@\r\n%@",self.textView.text,info];
}

@end
